package com.qdu.bussiness.service.impl;

import java.util.List;

import com.qdu.common.utils.id.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qdu.bussiness.mapper.BusInventoryMapper;
import com.qdu.bussiness.domain.BusInventory;
import com.qdu.bussiness.service.IBusInventoryService;

/**
 * 库存信息Service业务层处理
 * 
 * @author qdu
 * @date 2022-03-06
 */
@Service
public class BusInventoryServiceImpl implements IBusInventoryService 
{
    @Autowired
    private BusInventoryMapper busInventoryMapper;

    /**
     * 查询库存信息
     * 
     * @param inventoryId 库存信息主键
     * @return 库存信息
     */
    @Override
    public BusInventory selectBusInventoryByInventoryId(Long inventoryId)
    {
        return busInventoryMapper.selectBusInventoryByInventoryId(inventoryId);
    }

    /**
     * 查询库存信息列表
     * 
     * @param busInventory 库存信息
     * @return 库存信息
     */
    @Override
    public List<BusInventory> selectBusInventoryList(BusInventory busInventory)
    {
        return busInventoryMapper.selectBusInventoryList(busInventory);
    }

    /**
     * 新增库存信息
     * 
     * @param busInventory 库存信息
     * @return 结果
     */
    @Override
    public int insertBusInventory(BusInventory busInventory)
    {
        // 先查询库存中 是否存在 相同物料信息
        List<BusInventory> busInventoryList = busInventoryMapper.selectBusInventoryListCondition(busInventory);
        // 如果存在 数量++
        int res = 0;
        if(!busInventoryList.isEmpty()){
            if(busInventoryList.size() == 1){
                BusInventory nowInInventory = busInventoryList.get(0);
                BusInventory updateDto = new BusInventory();
                updateDto.setInventoryId(nowInInventory.getInventoryId());
                updateDto.setItemNum(nowInInventory.getItemNum() + busInventory.getItemNum());
                updateDto.setInventoryStatus(busInventory.getInventoryStatus());
                res = busInventoryMapper.updateBusInventory(updateDto);
            }
        }else{
            busInventory.setInventoryId(IdUtil.getId());
            res = busInventoryMapper.insertBusInventory(busInventory);
        }
        // 如果不存在 新增记录
        return res;
    }

    /**
     * 修改库存信息
     * 
     * @param busInventory 库存信息
     * @return 结果
     */
    @Override
    public int updateBusInventory(BusInventory busInventory)
    {
        return busInventoryMapper.updateBusInventory(busInventory);
    }

    /**
     * 批量删除库存信息
     * 
     * @param inventoryIds 需要删除的库存信息主键
     * @return 结果
     */
    @Override
    public int deleteBusInventoryByInventoryIds(Long[] inventoryIds)
    {
        return busInventoryMapper.deleteBusInventoryByInventoryIds(inventoryIds);
    }

    /**
     * 删除库存信息信息
     * 
     * @param inventoryId 库存信息主键
     * @return 结果
     */
    @Override
    public int deleteBusInventoryByInventoryId(Long inventoryId)
    {
        return busInventoryMapper.deleteBusInventoryByInventoryId(inventoryId);
    }
}
